//
//  TF_JoinMember.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/4/2.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TF_JoinMember: UIView {
    @objc public let avatar = UIImageView()
    @objc public let msg = UILabel()
    @objc public let joinMemberList = NSMutableArray()
     public let gl = CAGradientLayer()
     public let background = UIView()
    //成员加入
    @objc public var fire:Bool = false
     override init(frame: CGRect) {
         super.init(frame: frame)

         self.addSubview(self.background)
    
         self.background.layer.addSublayer(self.gl)
         

         self.avatar.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
       
         self.addSubview(self.avatar)
     
         
      
         self.msg.textAlignment = .left;

         self.msg.numberOfLines = 0;
         self.addSubview(self.msg)
         
       
         self.msg.textColor =  UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
     }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.background.frame = self.bounds
      
        self.background.layer.shadowColor = UIColor(red: 45/255.0, green: 171/255.0, blue: 255/255.0, alpha: 0.16).cgColor
        
        self.background.layer.shadowOffset = CGSize(width: 0, height: 1.5)
        self.background.layer.shadowRadius = 3
        self.background.layer.shadowOpacity = 1
        self.background.layer.cornerRadius = self.frame.size.height/2
        
        self.gl.frame = self.bounds
        self.gl.startPoint = CGPoint(x: 0.9892288446426392, y: 0.5)
        self.gl.endPoint = CGPoint(x: 0, y: 0.5)
       
        
       
        
         self.gl.colors = [UIColor(red: 255/255.0, green: 162/255.0, blue: 55/255.0, alpha: 1.0).cgColor, UIColor(red: 255/255.0, green: 83/255.0, blue: 64/255.0, alpha: 1.0).cgColor]
        self.gl.locations = [0,1]
        self.gl.cornerRadius = self.frame.size.height/2
        
        
        self.avatar.frame = CGRect(x: 5, y: (self.frame.size.height - 25)/2, width: 25, height: 25)
        self.avatar.layer.masksToBounds = true
        self.avatar.layer.cornerRadius = 25 / 2
      
        self.msg.frame = CGRect(x: self.avatar.frame.maxY+7, y: (self.frame.size.height - 20)/2, width: self.frame.size.width - 11, height: 20) 
    }
    @objc public func timeFireMethod(Y:CGFloat)
    {
        
          if (self.fire==false) {
                self.fire = true
                if (self.joinMemberList.count>0)
                {
                    self.alpha  =  0.8;
                    let dict =   self.joinMemberList[0] as! TFMember

                    self.avatar.sd_setImage(with: URL.init(string: dict.avatar))
                   // [self.avatar sd_setImageWithURL:[NSURL URLWithString:dict.avatar]];
                                
                    var nickname = dict.nickname as NSString
                           
                    if (nickname.length > 7) {
                        nickname = nickname.substring(to: 7) as NSString
                        nickname = (nickname as String) + "..." as NSString
                        nickname = (nickname as String) + " 进入" as NSString
 
                    }else if (nickname.length <= 3 ) {
                        nickname = (nickname as String) + " 进入直播间" as NSString
                 
                    }else{
                        nickname = (nickname as String) + " 进入" as NSString
                    }
                    

                    self.msg.attributedText = NSMutableAttributedString.getAttributed(string: nickname as String, font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0));
                             
                          
                            let size: CGSize = nickname.size(withAttributes: [NSAttributedString.Key.font: UIFont.PingFang(size: 14)])
                    
                    
                        
                                
                    let  joinMemberW:CGFloat   = 12 + 25 + 7 + size.width + 11
                    let  joinMemberH:CGFloat = 30
                                self.frame = CGRect(x: -joinMemberW, y: Y-joinMemberH-16, width: joinMemberW, height: joinMemberH)
                    UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) {
              
                        self.transform = CGAffineTransform(translationX:12+joinMemberW, y: 0)
                        
                    } completion: { Bool in
                        
                        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(1)) {
                            UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) {
                              
                                self.alpha = 0
                            } completion: { Bool in
                                self.fire = false
                                self.transform = CGAffineTransform(translationX: 0, y: 0);
                                if (self.joinMemberList.count>0) {
                                    self.joinMemberList.removeObject(at: 0)
                                }
                                if (self.joinMemberList.count>0) {
                                    self.timeFireMethod(Y: Y)
                                  
                                }
                            }
                        }
                        
                    }

                }
            }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
